1635C - Differential Sorting - CodeForces Solution


constructive algorithms greedy *1200

Please click on ads to support us..

Python Code:

for i in range(int(input())):
  n=int(input())
  a=list(map(int,input().split()))
  if a==sorted(a):
    print(0)
    continue
  if(a[-1]<0 or a[-1]<a[-2]):
    print(-1)
    continue
  print(n-2)
  for i in range(n-2):
    print(i+1,n-1,n)

C++ Code:

/***********************************************************************************************************************
                                  <<<<<<<<<<<<<<<<<<<<<<♥♥♥♥♥♥صلي على النبي وتبسم ♥♥♥♥♥♥>>>>>>>>>>>>>>>>>>>
    **********************************************************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <iterator>
#include <deque>
#include <queue>
#include <stack>
#include <utility>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <numeric>

using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define endl '\n'
#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(c) (c).begin(), (c).end()
#define rall(c) c.rbegin(), c.rend()
#define Sort(c) sort(all(c))
#define Reverse(c) reverse(all(c))
#define cin(c) for(auto&i:c)cin>>i;
#define cout(c) for(auto&i:c)cout<<i<<' '
#define cinpair(c) for(auto&i:c)cin>>i.first>>i.second
#define coutpair(c) for(auto&i:c)cout<<i.first<<' '<<i.second<<'\n'
#define sz(v) v.size()
#define F first
#define S second
#define pb push_back
#define fixed(n) fixed << setprecision(n)

bool IsPowerOf2(int x) {
    return (x && !(x & x - 1));
}

int computeXOR(int n) {
    if (n % 4 == 0)return n;
    if (n % 4 == 1)return 1;
    if (n % 4 == 2)return n + 1;
    else return 0;
}

int getFirstOneIdx(int n) {
    return log2(n & -n);
}

int getBit(int num, int idx) {
    return ((num >> idx) & 1) == 1;
}

int setBit1(int num, int idx) {
    return num | (1 << idx);
}

int setBit0(int num, int idx) {
    return num & ~(1 << idx);                // 110100, idx = 3  -->  110100 & ~000100 = 110100 & 111011
}

int flipBit(int num, int idx) {
    return num ^ (1 << idx);
}

int BinaryExponentiationREC(int a, int b) {
    if (!b)
        return 1;
    int ret = BinaryExponentiationREC(a, b / 2);
    return (b & 1 ? ret * ret * a : ret * ret);
}

ll BinaryExponentiation(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1)res *= a;
        a *= a;
        b /= 2;
    }
    return res;
}

ll ModularExponentiation(ll a, ll b, ll m) {
    ll res = 1;
    while (b) {
        if (b & 1)res = (res * a) % m;
        a = (a * a) % m;
        b /= 2;
    }
    return res;
}

ll gcd(ll a, ll b)  //O( log(max(a,b)) )
{
    //gcd (a,b)=gcd(b,a%b)
    // if b=0 stop
    while (b != 0) {
        ll a2 = a;
        a = b;
        b = a2 % b;
    }
    return a;
}

ll lcm(ll a, ll b) {
    //return ((a * b) / gcd(a, b));
    return (a / gcd(a, b)) * b;
}

ll Modulo(ll a, ll b) {
    return (((a % b) + b) % b);
}

void solve() {
    int n; cin>>n;
    vector<ll>v(n);
    cin(v);
    if (v[n-1]<v[n-2])
    {
        cout<<-1;
        return;
    }
    if (v[n-1]>=0)
    {
        cout<<n-2<<endl;
        for(int i=0;i<n-2;i++)cout<<i+1<<' '<<n-1<<' '<<n<<endl;
        return;
    }
        for (int i=0;i<n-1;i++)
        {
            if (v[i]>v[i+1])
            {
                cout<<-1;
                return;
            }
        }
        cout<<0;
}

int main() {
    FAST
    ll testcases = 1;
    cin >> testcases;
    while (testcases--) {
        solve();
        cout << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem